class canvasEvent {
  constructor (param) {
    this.canvas = param.canvas
    this.init()
  }
  init () {
    console.log("-------------------初始化 canvas 事件----------------------")
    this.canvas.addEventListener('click', ()=>{
      alert("oooo")
    }, false);
  }

  addCanvasClickListener(canvas, mesh) {
    let camera = sceneMrg.camera;
    const raycaster = new THREE.Raycaster();
    const mouse = new THREE.Vector2();
    const renderer = sceneMrg.renderer;
    return
    renderer.domElement.addEventListener('click', (event) => {
        // 转换鼠标位置为标准化坐标
        mouse.x = (event.clientX / window.innerWidth) * 2 - 1;
        mouse.y = -(event.clientY / window.innerHeight) * 2 + 1;
        raycaster.setFromCamera(mouse, camera);

        // 检测交互
        const intersects = raycaster.intersectObject(mesh, true);
        if (intersects.length > 0) {
            const intersect = intersects[0];
            const uv = intersect.uv;

            // 转换 UV 坐标为 Canvas 像素坐标
            uv.multiplyScalar(canvas.width);
            const x = Math.floor(uv.x);
            const y = Math.floor(canvas.height - uv.y);

            console.log(`Clicked on Canvas at (${x}, ${y})`);

            // 在此处处理 Canvas 上的点击事件
            const ctx = canvas.getContext('2d');
            const pixel = ctx.getImageData(x, y, 1, 1).data;
            console.log(`Pixel data at (${x}, ${y}):`, pixel);

            // 确保绘制绿色方块
            ctx.fillStyle = 'green';
            ctx.fillRect(x - 50, y - 50, 300, 300);

            // 更新材质
            const texture = new THREE.CanvasTexture(canvas);
            mesh.material[0].map = texture;  // 假设第一个材质使用了 Canvas 纹理
            texture.needsUpdate = true;

            console.log("材质更新成功");
        } else {
            console.log("未检测到交互");
        }
    });
}
}

export default canvasEvent